// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import store from "./vuex/store"
import Api from "./api/index"

Vue.filter('time', function (value) {//时间过滤器
  var now = new Date().getTime(),
    oldTime = new Date(value).getTime(),
    difference = now - oldTime,
    result='',
    minute = 1000 * 60,
    hour = minute * 60,
    day = hour * 24,
    halfamonth = day * 15,
    month = day * 30,
    year = month * 12,
    _year = difference/year,
    _month =difference/month,
    _week =difference/(7*day),
    _day =difference/day,
    _hour =difference/hour,
    _min =difference/minute;
  if(_year>=1) {result= "发表于 " + ~~(_year) + " 年前"}
  else if(_month>=1) {result= "发表于 " + ~~(_month) + " 个月前"}
  else if(_week>=1) {result= "发表于 " + ~~(_week) + " 周前"}
  else if(_day>=1) {result= "发表于 " + ~~(_day) +" 天前"}
  else if(_hour>=1) {result= "发表于 " + ~~(_hour) +" 个小时前"}
  else if(_min>=1) {result= "发表于 " + ~~(_min) +" 分钟前"}
  else result="刚刚";
  return result;
});

Vue.config.productionTip = false;

router.beforeEach((to,from,next)=>{
  if(to.path!=="/login"){
    if(!store.state.auth.token){
      console.log(store.state.auth.token);
      next({path:"/login"});
    }else {
      Api.userInfo().then((response)=>{
        if(response.status===200){
          store.commit("USERINFO_SUCCESS",{user:response.data.user});
          next();
        }else {
          alert(response.data.error);
        }
      })
    }
  }else {
    next();
  }

});



Vue.use(ElementUI);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App },
  store
});

